package com.bookstore.web;

import com.bookstore.bean.ShoppingCart;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Admin on 2016/8/14.
 */
public class BookStoreWebUtils {
    /**
     * 获取购物车对象: 从 session 中获取, 若 session 中没有改对象.
     * 则创建一个新的购物车对象, 放入到 session 中.
     * 若有, 则直接返回.
     * @param request
     * @return
     */
    public static ShoppingCart getShoppingCart(HttpServletRequest request){
        HttpSession httpSession=request.getSession();
        ShoppingCart sc= (ShoppingCart) httpSession.getAttribute("ShoppingCart");
        if(sc==null){
            sc=new ShoppingCart();
            httpSession.setAttribute("ShoppingCart",sc);
        }
        return sc;
    }
}
